package masking.wechat.sdk.mp;

import java.io.IOException;

import masking.wechat.sdk.MPAccount;
import masking.wechat.sdk.WechatUserInfo;
import masking.wechat.sdk.util.HttpUtils;
import masking.wechat.sdk.util.JacksonUtils;

public class MpUtil {
	
	public static final String MP_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info";    
	
	/**
	 * 根据微信公众号openId获取UserInfo
	 */
	public static WechatUserInfo getUserInfo(MPAccount mp, String openid) throws IOException {
		StringBuilder url = new StringBuilder(MP_USER_INFO_URL);
		url.append("?access_token=");
		url.append(MpAccessTokenUtil.get(mp));
		url.append("&openid=");
		url.append(openid);
		
		String response = HttpUtils.get(url.toString());
		return JacksonUtils.getInstance().fromJson(response, WechatUserInfo.class);
	}
}
